package com.xyhuo.leetcode.y2025;

import com.xyhuo.leetcode.common.TreeNode;

public class KthSmallest {

    public int kthSmallest(TreeNode root, int k) {
        this.k = k;
        cal(root);
        return ans;
    }

    private int ans = -1;

    private int k = 0;

    private void cal(TreeNode root) {
        if (null == root || ans != -1) {
            return;
        }
        cal(root.left);
        k--;
        if (k == 0) {
            ans = root.val;
        }
        cal(root.right);
    }

    public static void main(String[] args) {
        KthSmallest k = new KthSmallest();
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        TreeNode t4 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t6 = new TreeNode(6);
        t5.left = t3;
        t5.right = t6;
        t3.left = t2;
        t3.right = t4;
        t2.left = t1;

        k.kthSmallest(t5, 3);

    }
}
